layui.use(['jquery','layer','form','table','laydate','upload'],function(){
    var $ = layui.$,layer = layui.layer,form = layui.form,table = layui.table,laydate = layui.laydate,upload = layui.upload;

    form.on('submit(sub)',function(data){
        var user = data.field;
        $.ajax({
            url: '/user/editUser'
            ,data: user
            ,dataType: 'json'
            ,success:function(data){
                var layer = parent.layer;
                layer.close(layer.index);
                parent.getUsers(user.userId);
                layer.msg('修改成功');
            }
        });
    });

    //取消
    cancel = function cancel(){
        var layer = parent.layer;
        layer.close(layer.index);
    }


//获取父页面用户id
var userId = parent.$('#userId').val();
$('input[name=userId]').val(userId);
$.ajax({
    url: '/user/getUsers'
    ,data: {'userId': userId}
    ,dataType: 'json'
    ,success:function(data){
        var user = data.data[0];
        $('input[name=account]').val(user.account);
        $('input[name=nickname]').val(user.nickname);
        $('input[name=userCode]').val(user.userCode);
        $('input[name=studyDate]').val(user.studyDate);
        $('input[name=idCard]').val(user.idCard);
        $('input[name=phone]').val(user.phone);
        $('input[name=birthDate]').val(user.birthDate);
        $('input[name=email]').val(user.email);
        $('textarea[name=freestyle]').val(user.freestyle);
        $('textarea[name=remarks]').val(user.remarks);
        $('#avatarUrl').attr('src',user.avatarUrl);
        $('input[name=avatarUrl]').val(user.avatarUrl);
        if(user.sex == 1){
            $('input[name=sex][value="1"]').attr('checked',true);
        }else if(user.sex == 2){
            $('input[name=sex][value="2"]').attr('checked',true);
        }
        form.render();
    }
});

//修改头像
var uploadInst = upload.render({
    elem: '#avatarUrl'
    ,auto: true
    ,url: '/Upload/uploadImages'
    ,field: 'file'
    ,choose:function(obj){
        var files = obj.pushFile();
        obj.preview(function(index,file,result){
            $('#avatarUrl').attr('src',result);
        })
    }
    ,done:function (res) {
        $('input[name=avatarUrl]').val(res.data);
    }
});

});